var notebook_view = {
init: 
    function () {
        notebook_view.update_note_list();
        notebook_view.bind_add_btn_click();
    },

rename_note:
    function (id, new_title) {
        rename_note_do(
            id, new_title,
            function () {
                console.log(id + ',' + new_title)
                $('#'+id+'_note-item')
                    .find('.note-item-title-replacement')
                        .replaceWith(new_title);             
            },
            function () {
                notify('Rename Note Error.')
            }
            )
    },

update_note_list: 
    function  () {
        get_note_list_do(
        function (state, data) {
            var notes = data;
            var notes_array = new Array();
            for (var i = 0; i < notes.length; i += 1) {   
                notes_array.push(
                    form_note(notes[i].id, notes[i].title));
            }

            $('#notebook-view-content').html(notes_array.join(''));
            for (var i = 0; i < notes.length; i += 1) {   
                $('#'+ notes[i].id+'_note-item').qtip({
                    content: notes[i].title,
                    position: {
                      corner: {
                        tooltip: 'topMiddle',
                         target: 'bottomMiddle',
                      }
                   }

                });
            }
            //bind actions
            notebook_view.bind_remove_btn_click();
            notebook_view.bind_note_item_hover();
            notebook_view.bind_note_item_icon_click();
            notebook_view.bind_note_item_title_dblclick();
        },
        function (state, data){
            notify('Load Notebook fail.');
        });
    },

bind_note_item_title_dblclick:
    function () {
        $('.note-item-title').unbind();
        $('.note-item-title').dblclick(function () {
            var old = $(this).text();
            $(this).html('<textarea class="note-item-title-replacement">'+old+'</textarea>');
            $(this).find('.note-item-title-replacement').click(
                function (){
                    return false;
                });
        });
    },


bind_remove_btn_click:
    function () {
        $('#note-block-content .note-item-remove-btn').unbind();
        $('#note-block-content .note-item-remove-btn').click(function (){
            var note_id = $($(this).parent()).attr('id').toString().split('_')[0];
            var title = $('#'+note_id+'_note-item').find('.note-item-title').text()
            if(confirm('Are you sure you want to permanently delete `'
                    + title + '` ?'))
            {
                remove_note_do(note_id, 
                function (state, data) {
                    notebook_view.update_note_list();
                    if (global.crr_note_id == data['id']){
                        //switch_page(0);
                        global.crr_note_id = -1;
                        global.crr_note_title = '';
                    }
                    notify('Remove notebook `'
                        + data['title'] + '` done. You can not Undo yet.');
                },
                function (state, data) {
                    notify('Remove notebook fail.');    
                });
            }
            return false;
        }); 
    },

bind_add_btn_click:
    function () {
        $('#note-new-btn').click(function (){
            var title = prompt('Create a new Notebook:');
            if (title == null) return;
            add_note_do(title,
                function (state, data) {
                    notebook_view.update_note_list();
                    note_view.update_block_list(data['id']);
                    global.crr_note_id = data['id'];
                    global.crr_note_title = data['title'];
                    //switch_page(1);
                    notify('Add notebook `'
                        + global.crr_note_title +'` done.');
                },
                function (state, data){
                    notify('Add notebook failed.');
                });
        });
    },

bind_note_item_hover:
    //BUG: event was not triggered
    function () {
        $('.note-item').hover(
        function(){
            $(this).find('.note-item-remove-btn').show() 
        },
        function (){ 
            $(this).find('.note-item-remove-btn').hide() 
        });
    },

bind_note_item_icon_click:
    function () {
        $('.note-item-icon-wrap').unbind();    
        $('.note-item-icon-wrap').click(function () {
            //save crrent editor content
            global.ue.save_content();
            global.ue.close();

            var note_item = $(this).parents('.note-item');
            var note_id = note_item.attr('id').toString().split('_')[0];
            var note_title = note_item.find('.note-item-title').text();
            if (!global.open_notes.hasOwnProperty(note_id))  {
                global.open_notes[note_id] = new note_view(note_id)
                global.open_notes[note_id].update_block_list();
                $('#tabs').tabs( 'add' , global.open_notes[note_id].div, note_title );
            }

            global.crr_note_id = note_id;
            global.crr_note_title = note_title;
            $('#tabs').tabs( 'select' , global.open_notes[note_id].div )
            //switch_page(2);
        });
    },

apply_filter:
    function (filter) {
        $('.block-item').each(function () {
            if ($(this).text().search(filter_text) == -1) {
                $(this).parent().parent().parent().fadeOut();
            } else {
                $(this).parent().parent().parent().fadeIn();
            }
        });
    }
}
